/*--------------------------------------------------------------------------------
	DESCRIPTION: Cleaning European Social Survey (ESS)

--------------------------------------------------------------------------------*/

clear
use "$data_dir/raw/ESS Rounds 1-10.dta"

clonevar S009 = cntry

merge m:1 S009 using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "ESS"
gen wave = essround

rename weight weight_old
gen weight = pspwght

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = .

gen trust_justice = 1 if trstlgl>=5 & trstlgl!=.
replace trust_justice = 0 if trstlgl<5 & trstlgl!=.

gen trust_police = 1 if trstplc>=6 & trstplc!=. 
replace trust_police = 0 if trstplc<6 & trstplc!=.

gen trust_civil_service = .

gen trust_local_govt = .

gen trust_parties = 1 if trstprt>=3 & trstprt!=.
replace trust_parties = 0 if trstprt<3 & trstprt!=.

gen trust_parliament = 1 if trstprl>=5 & trstprl!=.
replace trust_parliament = 0 if trstprl<5 & trstprl!=.

gen trust_others = 1 if ppltrst>=5 & ppltrst!=.
replace trust_others = 0 if ppltrst<5 & ppltrst!=.

gen trust_military = .

pca trust_justice trust_police trust_parliament, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

gen survey_year = .
forvalues i = 1/10 {
	replace survey_year = 2000+(2*`i') if essround==`i'
}
replace survey_year = . if essround==.

gen yob = yrbrn
replace yob = -99 if yrbrn==.|yrbrn==.a|yrbrn==.b|yrbrn==.c

rename age ageess
gen age = survey_year - yob if yob!=-99

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if gndr==1
replace sex = 2 if gndr==2
replace sex = -99 if gndr==.

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(rlgdnm, 66)
replace religion = 3 if inlist(rlgdnm, 5)
replace religion = 4 if inlist(rlgdnm, 1, 2, 3, 4)
replace religion = 5 if inlist(rlgdnm, 6)
replace religion = 6 if inlist(rlgdnm, 7, 8)
replace religion = -99 if inlist(rlgdnm, 77, 99) | rlgdnm==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

cap drop edu
gen edu = 1 if edulvla==1
replace edu = 1 if inlist(edulvlb, 113, 129) & edu==.
replace edu = 2 if (edulvla==2|edulvla==3) & edu==.
replace edu = 2 if (edulvlb>200 & edulvlb<=323) & edu==.
replace edu = 3 if (edulvla==4|edulvla==5) & edu==.
replace edu = 3 if (edulvlb>=400 & edulvlb<=800) & edu==.
replace edu = 4 if edulvlb==0 & edu==.
replace edu = -99 if edu==.

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen isco_ess = isco08
replace isco_ess = iscoco if isco_ess==. & iscoco!=.
tostring isco_ess, replace
gen occupation = regexs(0) if regex(isco_ess, "[0-9]")
replace occupation = "0" if isco08>=0 & isco08<=310
replace occupation = "0" if iscoco==100
replace occupation = "-99" if occupation==""
destring occupation, replace

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = 1 if empl==1|empl==2
replace employment_status = 0 if empl==3
replace employment_status = -99 if employment_status==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = hinctnta
replace income = 1 if (hinctnt==1|hinctnt==2) & income==.
replace income = 2 if hinctnt==3 & income==.
replace income = 3 if hinctnt==4 & income==.
replace income = 4 if hinctnt==5 & income==.
replace income = 5 if hinctnt==6 & income==.
replace income = 6 if hinctnt==7 & income==.
replace income = 7 if hinctnt==8 & income==.
replace income = 8 if hinctnt==9 & income==.
replace income = 9 if hinctnt==10 & income==.
replace income = 10 if (hinctnt==11|hinctnt==12) & income==.
replace income = -99 if hinctnt==.|hinctnta==.

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

gen ideology = lrscale
replace ideology = 1 if lrscale==0
replace ideology = -99 if ideology==.

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

rename marital maritaless
gen marital = 1 if maritaless==1|maritala==1|maritalb==1|maritalb==2|marsts==1|marsts==2
replace marital = 2 if (maritaless>1 & maritaless!=.) | (maritala>1 & maritala!=.) | (maritalb>2 & maritalb!=.) | ///
					   (marsts>2 & marsts!=.)
replace marital = -99 if maritaless==. & maritala==. & maritalb==. & marsts==.

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = -99

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = 1 if brncntr==1
replace born_country = 0 if brncntr==2

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/ess_clean.dta", replace
